package edu.zhku.ttms.view.dialog;

import edu.zhku.ttms.domain.TrafficPoliceman;
import edu.zhku.ttms.service.TrafficPolicemanService;
import edu.zhku.ttms.service.impl.TrafficPolicemanServiceImpl;
import edu.zhku.ttms.utils.CheckUtil;
import edu.zhku.ttms.view.ViewController;
import edu.zhku.ttms.view.font.LeftLabelFont;
import edu.zhku.ttms.view.panel.content.TrafficPoliceManagementPanel;

import javax.swing.*;
import java.awt.*;

/**
 * @author 泽
 * @since 2021/4/26 21:46
 */
public class TrafficPolicemanAddDialog extends JDialog {

    static TrafficPolicemanService trafficPolicemanService = new TrafficPolicemanServiceImpl();

    public TrafficPolicemanAddDialog() {
        super(ViewController.getFrame(), "添加交警", true);
        this.setLayout(null);
        this.setBounds(560, 395, 440, 200);

        Font font = new LeftLabelFont();

        int x = 10;
        int y = 0;
        int fieldWidth = 300;
        int labelWidth = 100;
        int buttonWidth = 100;
        int height = 25;
        int margin = 5;

        int incrementY = height + margin;

        JLabel policeCodeLabel = new JLabel("交警代号");
        policeCodeLabel.setFont(font);
        policeCodeLabel.setBounds(x, y, labelWidth, height);
        this.add(policeCodeLabel);

        JTextField policeCodeInput = new JTextField();
        policeCodeInput.setBounds(x + labelWidth, y, fieldWidth, height);
        this.add(policeCodeInput);

        JLabel nameLabel = new JLabel("姓名");
        nameLabel.setFont(font);
        nameLabel.setBounds(x, y + incrementY, labelWidth, height);
        this.add(nameLabel);

        JTextField nameInput = new JTextField();
        nameInput.setBounds(x + labelWidth, y + incrementY, fieldWidth, height);
        this.add(nameInput);

        JLabel branchLabel = new JLabel("所属分局");
        branchLabel.setFont(font);
        branchLabel.setBounds(x, y + 2 * incrementY, labelWidth, height);
        this.add(branchLabel);

        JTextField branchInput = new JTextField();
        branchInput.setBounds(x + labelWidth, y + 2 * incrementY, fieldWidth, height);
        this.add(branchInput);

        JLabel tip = new JLabel();
        tip.setForeground(Color.RED);
        tip.setFont(font);
        tip.setBounds(x + labelWidth, y + 3 * incrementY, fieldWidth, height);
        this.add(tip);

        JButton policemanAddButton = new JButton("添加");
        policemanAddButton.addActionListener(e -> {
            String policeCode = policeCodeInput.getText();
            String name = nameInput.getText();
            String branch = branchInput.getText();
            String res = trafficPolicemanService.add(new TrafficPoliceman(policeCode, name, branch));
            if (CheckUtil.isSuccess(res)) {
                this.setVisible(false);
                ViewController.changeView(new TrafficPoliceManagementPanel());
            } else {
                tip.setText(res);
            }
        });
        policemanAddButton.setBounds(x + labelWidth, y + 4 * incrementY, buttonWidth, height);
        this.add(policemanAddButton);
    }
}
